﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Columns;
using DevExpress.Data.Filtering;
using Model;

namespace TanHungLong
{
    public partial class FrmLog : DevExpress.XtraEditors.XtraForm
    {
        #region variant
        #endregion variant

        //=====================================
        #region form
        public FrmLog()
        {
            InitializeComponent();
            this.KeyPreview = true;

            this.Load += new EventHandler(FrmLog_Load);
            this.Shown += new EventHandler(FrmLog_Shown);

            this.btnSearch.Click += new EventHandler(btnSearch_Click);

            this.KeyDown += new KeyEventHandler(FrmLog_KeyDown);
        }

        private void FrmLog_KeyDown(object sender, KeyEventArgs e)
        {
            if (this.btnClose.Visible && ((e.KeyCode == Keys.Q && Control.ModifierKeys == Keys.Control) || (e.KeyCode == Keys.F10)))
                this.Close();
            else
                if (this.btnRefresh.Visible && ((e.KeyCode == Keys.R && Control.ModifierKeys == Keys.Control) || (e.KeyCode == Keys.F5)))
                    RefreshData();
                else
                    if ((e.KeyCode == Keys.F && Control.ModifierKeys == Keys.Control) || (e.KeyCode == Keys.F3))
                        FilterData();
        }

        private void FrmLog_Shown(object sender, EventArgs e)
        {
            this.dtTuNgay.Focus();
        }

        private void FrmLog_Load(object sender, EventArgs e)
        {
            this.btnRefresh.Click += new EventHandler(btnRefresh_Click);
            this.btnClose.Click += new EventHandler(btnClose_Click);

            this.grdView.RowCellStyle += new RowCellStyleEventHandler(grdView_RowCellStyle);

            MyStyle.SetGridStyle(this.grdView, false);
            Utility.InitControl(this.repLookUpNhanVienID);

            this.dtTuNgay.DateTime = DateTime.Now;
            this.dtDenNgay.DateTime = DateTime.Now;

            RefreshData();
        }
        #endregion form

        //=====================================
        #region control
        #endregion control

        //=====================================
        #region button
        private void btnSearch_Click(object sender, EventArgs e)
        {
            FilterData();
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            RefreshData();
            FilterData();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion button

        //=====================================
        #region grid
        private void grdView_RowCellStyle(object sender, RowCellStyleEventArgs e)
        {
            string s = this.grdView.GetRowCellDisplayText(e.RowHandle, "TacVu");
            Color color = Color.Black;
            switch (s)
            {
                case "Đăng nhập":
                    color = Color.FromArgb(22, 0, 236);
                    break;
                case "Đăng xuất":
                    color = Color.FromArgb(185, 202, 0);
                    break;
                case "Thêm":
                    color = Color.FromArgb(0, 212, 15);
                    break;
                case "Sửa":
                    color = Color.FromArgb(212, 110, 0);
                    break;
                case "Xóa":
                    color = Color.FromArgb(228, 0, 0);
                    break;
            }

            e.Appearance.ForeColor = color;
        }
        #endregion grid

        //=====================================
        #region function
        private void FilterData()
        {
            DateTime TuNgay = this.dtTuNgay.DateTime;
            DateTime DenNgay = this.dtDenNgay.DateTime;

            if (TuNgay > DenNgay)
            {
                TuNgay = DenNgay;
                DenNgay = this.dtTuNgay.DateTime;
            }

            this.dtTuNgay.DateTime = TuNgay;
            this.dtDenNgay.DateTime = DenNgay;

            string sTuNgay = "CONVERT(datetime,'" + TuNgay.ToString("yyyy-MM-dd") + " 00:00:00',120)";
            string sDenNgay = "CONVERT(datetime,'" + DenNgay.ToString("yyyy-MM-dd") + " 23:59:59',120)";

            string where = "DATC>=" + sTuNgay + " AND DATC<=" + sDenNgay;
            LogBOCollection logBOCollection = LogBOCollection.GetLogBOCollection(where, "NhanVienID");
            this.bdSource.DataSource = logBOCollection;
            this.grdView.CheckLoaded();

            int length = this.grdView.RowCount;
            int index = 0;
            int stt = 0;
            for (int i = 0; i < length; i++)
            {
                index = this.grdView.GetVisibleRowHandle(i);
                if (index >= 0)
                    this.grdView.SetRowCellValue(index, "STT0", ++stt);
            }

            this.grdView.BestFitColumns();

            this.grdView.ViewCaption = "LỊCH SỬ LÀM VIỆC TỪ NGÀY " + TuNgay.ToString("dd/MM/yyyy") + " ĐẾN NGÀY " + DenNgay.ToString("dd/MM/yyyy");
        }

        private void RefreshData()
        {
            DangNhapFullBOList dangNhapFullBOList = DangNhapFullBOList.GetDangNhapFullBOList();
            dangNhapFullBOList.InsertCustomValue(-1, "admin");
            Utility.SetData(this.repLookUpNhanVienID, dangNhapFullBOList, dangNhapFullBOList.Count);
        }
        #endregion function
    }
}